# |Info|
# Written by Bryan O'Connell, May 2013
# Purpose: Collects files to create a build of wsubi.
#
# Sample: wsubi-build.ps1 -source "C:\Projects\wsubi" -build_folder "C:\Users\Bryan\Desktop\wsubi" -version "1.5"
#
# Params:
#  -source: The root location of your wsubi Visual Studio project. This script 
#  assumes that the folder you specify here contains your 'bin' folder.
#
#  -build_folder: The place where the build will be created. If an old version 
#  exists, it will be deleted. If the folder you specify doesn't exist, it 
#  will be created.
#
#  -version: The version number of the build you want to create.
#
# |Info| 

[CmdletBinding()]
Param (
    [Parameter(Mandatory=$true,Position=0)]
    [string]$source,
    
    [Parameter(Mandatory=$true,Position=1)]
    [string]$build_folder,
    
    [Parameter(Mandatory=$true,Position=2)]
    [string]$version
)

#-----------------------------------------------------------------------------#

# if exists, delete destination folder and contents
if (Test-Path $build_folder)
{
    Remove-Item $build_folder -force -recurse;
}

# create empty destination folder
md -Path $build_folder;

# copy needed files from source to destination
Copy-Item ($source + "\README.txt") $build_folder;
Copy-Item ($source + "\sample-queue.txt") $build_folder;
Copy-Item ($source + "\shortcuts.wsbi") $build_folder;

Copy-Item ($source + "\scripts") $build_folder -recurse;
COpy-Item ($source + "\output") $build_folder;

Copy-Item ($source + "\bin\Release\wsubi.exe") $build_folder;
Copy-Item ($source + "\bin\Release\wsubi.exe.config") $build_folder;

# Code for zipping sampled from the following post:
# -http://viziblr.com/news/2012/8/18/zip-up-a-folder-with-powershell-3-the-easy-way.html
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" );
$zipfile = $build_folder + ".zip";
$compressionLevel = [System.IO.Compression.CompressionLevel]::Fastest;
$includebasedir = $true;
[System.IO.Compression.ZipFile]::CreateFromDirectory($build_folder, $zipfile, $compressionLevel, $includebasedir);

#Remove any past zip packages of the same version, then rename 
#the zip file to include the specified version number.
$zipped_build = $build_folder + "-" + $version + ".zip";

if (Test-Path $zipped_build)
{
    Remove-Item ($zipped_build) -force;
}

Rename-Item $zipfile ("wsubi-" + $version + ".zip");

# done!
write-Output "Build complete :D. Press any key to quit ...";
$quit = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");
